<?php
/*
---------LICENSE----------
You may modify this code as you see fit, for your own personal
use on your or your guild's website, providing you leave this license
intact. You may not make this file or any derivative of this file, or 
any archive containing this file or a derivative of this file available 
for download on any website without the express written consent of an
authorized representative of WoWSim.
*/

header('Content-type: text/html; charset=UTF-8') ;
include_once("./WSArmoryCore.php");

//Diagnostics to run, if "diagnostics=true" is in the URL
if($_GET['diagnostics']) {
  echo '<em>-----WSArmory.php-----</em><br>';
  $passStr = "<strong><font color=\"#55aa55\">[PASS]</font></strong>";
  $failStr = "<strong><font color=\"#aa5555\">[FAIL]</font></strong>";
  
  $char = WSArmory_getCharacterSheetXML("http://www.wowarmory.com","Frewdicey","Azjol-Nerub");
  $guild = WSArmory_getGuildXML("http://www.wowarmory.com","Primoris","Azjol-Nerub");

  echo "Arena Team Mates Test...";
  if(WSArmory_getArenaTeamMates($char))
    echo $passStr."<br>"; else echo $failStr."<br>";
  echo "Arena Team Mate Guilds Test...";
  if(WSArmory_getArenaTeamMateGuilds($char))
    echo $passStr."<br>"; else echo $failStr."<br>";
}

//Get an array of names of a character's arena team mates. The objects in the array are strings, and there are no duplicates,
// even if a particular character is on more than one of this character's teams.
function WSArmory_getArenaTeamMates($characterXML) {
  if(!$characterXML || !$characterXML->characterInfo[0])
    WSArmory_errorXML("Invalid Argument! Must be character XML",$characterXML->asXML());
  $result = array();
  if(!$characterXML->characterInfo[0]->character[0] || !$characterXML->characterInfo[0]->character[0]->arenaTeams[0]) {
    return $result;// no teams
  }
  
  foreach($characterXML->characterInfo[0]->character[0]->arenaTeams[0]->children() as $a) 
    {
      foreach($a->members[0]->children() as $b) 
	{
	  if(strcmp($b['name'],$characterXML->characterInfo[0]->character[0]['name']) != 0) {
	    $alreadyPresent = false;
	    foreach($result as $c) 
	      {		
		$compareResult = strcmp($c,$b['name']);	      	
		if($compareResult == 0) {
		  $alreadyPresent = true;
		}
	      }
	    if(!$alreadyPresent) {
	      $result[] = (string)$b['name'];
	    }
	    
	  }
	}
    }
  return $result;
}

//Get a set of guilds of this character's arena team mates. The objects in the array are strings, and there
// are no duplicates. This is useful for crawling many characters in a realm (you can crawl from a character
// to its guildies, to their armory teammates, to their guildies, etc...
function WSArmory_getArenaTeamMateGuilds($characterXML) {
  if(!$characterXML || !$characterXML->characterInfo[0])
    WSArmory_errorXML("Invalid Argument! Must be character XML",$characterXML->asXML());
  $result = array();
  if(!$characterXML->characterInfo[0]->character[0] || !$characterXML->characterInfo[0]->character[0]->arenaTeams[0]) {
    return $result;// no teams
  }
  foreach($characterXML->characterInfo[0]->character[0]->arenaTeams[0]->children() as $a) 
    {
      foreach($a->members[0]->children() as $b) 
	{
	  $g = (string)$characterXML->characterInfo[0]->character['guildName'];
	  if(strcmp($b['guild'],$g) != 0) {
	    $alreadyPresent = false;
	    foreach($result as $c) 
	      {		
		$compareResult = strcmp($c,$b['guild']);	      	
		if($compareResult == 0) {
		  $alreadyPresent = true;
		}
	      }
	    if(!$alreadyPresent) {
	      $result[] = (string)$b['guild'];
	    }
	    
	  }
	}
    }
  return $result;
}

function WSArmory_getCharacterStats($characterXML) {

}

?>